package org.cjlm.ui.template;

import org.cjlm.MLocator;
import org.cjlm.itf.TemplateDAO;
import org.cjlm.ui.pub.MButton;
import org.cjlm.ui.pub.MCardLayout;
import org.cjlm.ui.pub.MInternalFrame;
import org.cjlm.vo.template.TemplateMVO;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.util.List;

public class AbsManagerUIFrm extends MInternalFrame {
    private TemplateMVO mvo;
    // UI组件
    private final MCardLayout cardLayout = new MCardLayout();
    private final JPanel mainPanel = new JPanel(cardLayout);
    private BillCardPanel cardPanel = null;
    private BillListPanel listPanel = null;

    public AbsManagerUIFrm(String mid, String title, boolean max, int width, int height) {
        super(title, max, width, height);
        // 主面板设置
        JPanel contentPane = new JPanel(new BorderLayout(10, 10));
        contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));
        setContentPane(contentPane);
        initParam(mid);
        // 添加到主卡片面板
        mainPanel.add(listPanel,  "list");
        mainPanel.add(cardPanel,  "card");
        contentPane.add(mainPanel,  BorderLayout.CENTER);

        // 事件处理
        setupEventHandlers();
//        loadOrderData();
    }

    private void setupEventHandlers() {
        List<MButton> btnlist = listPanel.getButtons();
        for(MButton btn : btnlist) {
            btn.addActionListener(e -> {
                onButtonClick(btn);
            });
        }
        btnlist = cardPanel.getButtons();
        for(MButton btn : btnlist) {
            btn.addActionListener(e -> {
                onButtonClick(btn);
            });
        }
    }

    private void initParam(String mid) {
        TemplateMVO qvo = new TemplateMVO(); qvo.setId(mid);
        try {
            this.mvo = MLocator.getInstance(TemplateDAO.class).queryByPk(qvo);
            this.listPanel = new BillListPanel(this.mvo);
            this.cardPanel = new BillCardPanel(this.mvo);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void onButtonClick(MButton btn){
        if(btn.getType() != null && btn.getType() < 100) {
            onPreBtnClick(btn.getType());
        }else{
            onPrvBtnClick(btn);
        }
    }

    /**
     * 自定义按钮事件
     * @param btn
     */
    private void onPrvBtnClick(MButton btn) {
        JOptionPane.showMessageDialog(this,  "自定义按钮功能开发中"+btn.getCode());
    }

    /**
     * 预制按钮事件
     * @param type
     */
    private void onPreBtnClick(Integer type) {
        switch (type){
            case 1 -> onBtnAdd();//新增
            case 2 -> onBtnEdit();//修改
            case 3 -> onBtnDel();//删除
            case 4 -> onBtnSave();//保存
            case 5 -> onBtnCancel();//取消
            case 6 -> onBtnQuery();//查询
            case 7 -> onBtnBackList();//返回列表
            case 8 -> onBtnAddLine();//增行
            case 9 -> onBtnDelLine();//删行
            default -> JOptionPane.showMessageDialog(this,  "预制按钮开发中"+type);
        }
    }

    private void onBtnDelLine() {
        if("card".equals(cardLayout.getCurrentCardName())){
            cardPanel.deleteLine();
        }
    }

    private void onBtnAddLine() {
        if("card".equals(cardLayout.getCurrentCardName())){
            cardPanel.addNewLine();
        }
    }

    private void onBtnBackList() {
        if("card".equals(cardLayout.getCurrentCardName())){
            //列表
            cardLayout.show(mainPanel,  "list");
        }
    }

    private void onBtnCancel() {
        cardPanel.setEditMode(false);
    }

    private void onBtnSave() {
    }

    private void onBtnDel() {
    }

    private void onBtnEdit() {
        if("list".equals(cardLayout.getCurrentCardName())){
            //列表
            cardLayout.show(mainPanel,  "card");
        }

        cardPanel.setEditMode(true);
    }

    private void onBtnAdd() {
        if("list".equals(cardLayout.getCurrentCardName())){
            //列表
            cardLayout.show(mainPanel,  "card");
        }

        cardPanel.setEditMode(true);
    }

    private void onBtnQuery() {
        // 创建自定义查询对话框
        QueryDialog dialog = new QueryDialog(this.mvo);
        dialog.setVisible(true);

        if (dialog.isConfirmed()) {
            // 获取生成的SQL查询条件
            String whereClause = dialog.buildWhereClause();

            // 构建完整SQL（假设表名为your_table）
            String sql = "SELECT * FROM your_table";
            if (!whereClause.isEmpty()) {
                sql += " WHERE " + whereClause;
            }

            // 执行查询（此处打印示例）
            System.out.println(" 执行查询: " + sql);
            JOptionPane.showMessageDialog(null, "生成SQL:\n" + sql, "查询语句", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    @Override
    public String getDefaultTile() {
        return null;
    }
}
